<?php
declare (strict_types=1);

namespace app\index\controller;

use addons\Sms\facade\Sms;
use app\common\model\Captcha;
use app\common\model\User;
use app\common\tool\Redis;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Login extends Base
{
    public function index()
    {
        return View::fetch();
    }

    public function check(): Response
    {
        try {
            $param = $this->request->post('', '', $this->filter);
            $this->validate($param, 'Login');
            $user = User::where('code|mobile', $param['mobile'])->find();
            if (empty($user)) return Result::fail('账号不存在');

//            if ($user['password'] != md5($param['password'])) {
//                return Result::fail('密码错误');
//            }
            if ($user['is_lock'] == 1) {
                return Result::fail('账号已冻结');
            }
            if(isset($param['captcha']))
            {
                $captcha = Captcha::where(['account'=>$param['mobile'],'code'=>$param['captcha']])->find();
                if (!$captcha)
                {
                    return Result::fail('验证码错误');
                }
                if ($captcha->end_time<time())
                {
                    return Result::fail('验证码已过期');
                }
            }
            $time = time();
            $token = md5(json_encode($user) . $time . $this->request->ip());
            $user->token = $token;
            $user->last_time = $time;
            $user->last_ip = $this->request->ip();
            $user->save();
            cookie('token', $token);
            return Result::fastReturn(['code' => 1, 'msg' => '登录成功', 'token' => $token, 'url' => '/index/user/index.html']);
        } catch (\Exception $e) {
            return Result::fail($e->getError());

        }
    }

    public function send()
    {
        try {
            $param = $this->request->param();
//            $check = $this->validate($param, 'Send');
//            if ($check !== true) return json(['code' => -1, 'msg' => lang($check)]);

//            $time = time();
            $code = mt_rand(100000, 999999);

            $content = '您的验证码是' . $code . '。 5分钟之内有效, 请勿告诉他人';

            $result = Sms::send($param['mobile'], $content);
            if ($result['code'] == 200) {
                Captcha::create([
                    'account' => $param['mobile'],
                    'code' => $code,
                    'create_time' => time(),
                    'end_time' => time() + (5 * 60),
                    'type' => 'mobile',
                ]);
//                Redis::Instance()->set('captcha_' . $param['mobile'], $code, 300);
                return json(['code' => 1, 'msg' => '已发送']);
            } else {
                return json(['code' => -1, 'msg' => '发送失败']);
            }

        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function forget()
    {
        if ($this->request->isPost()) {
            $param = $this->request->post('', '', $this->filter);
            try {
                $this->validate($param, 'Forget');
                $user = User::where('mobile', $param['mobile'])->find();
                if (empty($user)) {
                    return Result::fail('该手机号尚未注册');
                }

                $token = md5(json_encode($user) . time());
                $user->token = $token;
                $type = 'password';
                $user->$type = md5($param['password']);
                $user->save();
                $msg = $type == 'password' ? '登录密码' : '支付密码';
                return Result::fastReturn([
                    'code' => 1,
                    'msg'  => '您的' . $msg . '已找回，请牢记您的新密码',
                    'url'  => url('login/index')->build()
                ]);
            } catch (\Exception $e) {
                return Result::fail($e->getError());
            }
        }
        return View::fetch();
    }


}
